<?php
/**
*
*   mcyt杨天音乐即时爬虫(废弃)
*
*/

class mserver{

    /**
    *   @param String $txt 要搜索的字符串
    */

    public function search($txt,$p=1){
        $txt=str_replace(' ','+',$txt);
        $ch=curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://up.mcyt.net/?search='.$txt.'&p='.$p);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $html=curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        
        curl_close($ch);

        if ($httpCode==404)
		{return '';}
        
		$dom = new DOMDocument();
		@$dom->loadHTML($html);
		$xpath = new DOMXPath($dom);
		$nodeList = $xpath->query("//li[@class='ser_8']/a");
        $res=[];
        foreach($nodeList as $node){

            $tmp=[];
            $tmp['name']=$node->attributes->getNamedItem('title')->nodeValue;
            preg_match('/\d+/',$node->attributes->getNamedItem('href')->nodeValue,$matchs);

            $tmp['id']=$matchs[0];

            Array_push($res,$tmp);
        }

        return $res;

    }

    /**
    * @param $id 
    */
    public function getInfo($id){
        $ch=curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://up.mcyt.net/p/'.$id.'.html');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $html=curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        
        curl_close($ch);

        if ($httpCode==404)
		{return '';}

        $dom = new DOMDocument();
		@$dom->loadHTML($html);
		$xpath = new DOMXPath($dom);

        $res=[];
        $res['id']=$id;
        $res['name']=$xpath->evaluate("//div[@class='content']/h2")->item(0)->nodeValue;

        return $res;
    }

    /**
    * @param $id
    */
    public function getUrl($id){
        $ch=curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://up.mcyt.net/p/'.$id.'.html');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $html=curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        
        curl_close($ch);

        if ($httpCode==404)
		{return '';}

        $dom = new DOMDocument();
		@$dom->loadHTML($html);
		$xpath = new DOMXPath($dom);

        
        $surl=$xpath->evaluate("//input[@name='name']")->item(0)->attributes->getNamedItem('value')->nodeValue;
        
        $url=get_headers($surl,1)['location'];

        return $url;
    }
}

?>